package me.arvin.qa.jmeter.plugins.dependscontroller;

import org.apache.jmeter.control.LoopController;
import org.apache.jmeter.control.TransactionController;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.property.JMeterProperty;
import org.apache.jmeter.testelement.property.NullProperty;
import org.apache.jmeter.threads.ThreadGroup;
import org.apache.jorphan.collections.HashTree;
import org.apache.jorphan.collections.HashTreeTraverser;
import org.apache.jorphan.collections.ListedHashTree;

public class DependsListenerTraverser implements HashTreeTraverser {

	static final DependsListenerTraverser instance = new DependsListenerTraverser();

	private DependsListenerTraverser() {}

	@Override
	public void addNode(Object node, HashTree subTree) {
		if (node instanceof TestElement) {
			if (node instanceof ThreadGroup || node instanceof LoopController) {
				subTree.add(DependsManageThreadListener.ControllerListener, new ListedHashTree());
			}
			JMeterProperty drProp = ((TestElement) node).getProperty(DependsController.DependsRelationList);
			if (!(drProp instanceof NullProperty)) {
				DependsManageThreadListener.setDependsRelation((DependsRelationProperty) drProp);

				if (node instanceof TransactionController) {
					subTree.add(DependsManageThreadListener.ControllerListener, new ListedHashTree());
				} else {
					subTree.add(DependsManageThreadListener.SampleListener, new ListedHashTree());
				}
			}
		}

	}

	@Override
	public void subtractNode() {

	}

	@Override
	public void processPath() {

	}

}
